package com.yuan.base.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yuan.base.R;


/**
 */
public class DialogUtil {

	/**
	 * 确认框
	 */
	public static Dialog confirm(Context context, String title, String content,
								 String positiveButtonTxt, final OnBtnClickListener positiveClickListener) {
		return confirm(context, title, content, positiveButtonTxt, positiveClickListener, null, null, null, null);
	}

	/**
	 * 确认框
	 */
	public static Dialog confirm(Context context, String title, String content,
								 String positiveButtonTxt, final OnBtnClickListener positiveClickListener,
								 String negativeButtonTxt, final OnBtnClickListener negativeClickListener) {
		return confirm(context, title, content, positiveButtonTxt, positiveClickListener, negativeButtonTxt, negativeClickListener, null, null);
	}

	/**
	 * 确认框
	 */
	public static Dialog confirm(Context context, String title, String content,
								 String positiveButtonTxt, final OnBtnClickListener positiveClickListener,
								 String negativeButtonTxt, final OnBtnClickListener negativeClickListener,
								 String neutralButtonTxt, final OnBtnClickListener neutralClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(Html.fromHtml("<font color=#3DA89D>" + title + "</font>")).setMessage(content);
		if (positiveButtonTxt != null) {
			builder.setNegativeButton(positiveButtonTxt, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (positiveClickListener != null) {
						positiveClickListener.onClick(dialog, which);
					}
				}
			});
		}
		if (negativeButtonTxt != null) {
			builder.setPositiveButton(negativeButtonTxt, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (negativeClickListener != null) {
						negativeClickListener.onClick(dialog, which);
					}
				}
			});
		}
		if (neutralButtonTxt != null) {
			builder.setNeutralButton(neutralButtonTxt, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (neutralClickListener != null) {
						neutralClickListener.onClick(dialog, which);
					}
				}
			});
		}
		AlertDialog alertDialog = builder.show();
		try {
			TextView mTitleView = alertDialog.getWindow().findViewById(R.id.alertTitle);
			mTitleView.setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
			mTitleView.setText(title);//只设置颜色时不生效,加上设置title时才会生效,原因不明
		} catch (Exception ignore) {

		}
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_text));
		alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
		return alertDialog;
	}

	/**
	 * 选择框
	 */
	public static Dialog select(Context context, String title, String[] items, final OnBtnClickListener itemClickListener) {
		return select(context, title, items, itemClickListener, null, null);
	}

	/**
	 * 选择框
	 */
	public static Dialog select(Context context, String title, String[] items, final OnBtnClickListener itemClickListener,
								String negativeButtonTxt, final OnBtnClickListener negativeClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(Html.fromHtml("<font color=#3DA89D>" + title + "</font>"))
				.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						if (itemClickListener != null) {
							itemClickListener.onClick(dialog, which);
						}
					}
				}).setNegativeButton(negativeButtonTxt, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (negativeClickListener != null) {
							negativeClickListener.onClick(dialog, which);
						}
					}
				});
		AlertDialog alertDialog = builder.show();
		try {
			TextView mTitleView = alertDialog.getWindow().findViewById(R.id.alertTitle);
			mTitleView.setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
			mTitleView.setText(title);//只设置颜色时不生效,加上设置title时才会生效,原因不明
		} catch (Exception ignore) {

		}
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_text));
		alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
		return alertDialog;
	}

	/**
	 * 多选框
	 */
	public static Dialog selectMulti(Context context, String title, String[] items, boolean[] selected, final OnMultiSelectListener onMultiSelectListener,
									 String positiveButtonTxt, final OnBtnClickListener positiveClickListener,
									 String negativeButtonTxt, final OnBtnClickListener negativeClickListener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(Html.fromHtml("<font color=#3DA89D>" + title + "</font>"))
				.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which, boolean isChecked) {
						if (onMultiSelectListener != null) {
							onMultiSelectListener.onMultiSelect(dialog, which, isChecked);
						}
					}
				})
				.setPositiveButton(negativeButtonTxt, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (negativeClickListener != null) {
							negativeClickListener.onClick(dialog, which);
						}
					}
				})
				.setNegativeButton(positiveButtonTxt, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (positiveClickListener != null) {
							positiveClickListener.onClick(dialog, which);
						}
					}
				});
		AlertDialog alertDialog = builder.show();
		try {
			TextView mTitleView = alertDialog.getWindow().findViewById(R.id.alertTitle);
			mTitleView.setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
			mTitleView.setText(title);//只设置颜色时不生效,加上设置title时才会生效,原因不明
		} catch (Exception ignore) {
		}
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_text));
		alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
		return alertDialog;
	}

	/**
	 * 输入框
	 */
	public static Dialog input(Context context, String title, String defaultText,
							   String positiveButtonTxt, final OnConfirmListener onConfirmListener,
							   String negativeButtonTxt, final OnBtnClickListener negativeClickListener) {
		final EditText userName = new EditText(context);
		userName.setText(defaultText);
		userName.setSelection(defaultText.length());
		AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(Html.fromHtml("<font color=#3DA89D>" + title + "</font>"))
				.setView(userName)//为对话框添加要显示的组件
				.setNegativeButton(positiveButtonTxt, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (onConfirmListener != null) {
							onConfirmListener.onConfirm(dialog, userName.getText().toString(), userName);
						}
					}
				})
				.setPositiveButton(negativeButtonTxt, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (negativeClickListener != null) {
							negativeClickListener.onClick(dialog, which);
						}
					}
				});
		AlertDialog alertDialog = builder.show();
		try {
			TextView mTitleView = alertDialog.getWindow().findViewById(R.id.alertTitle);
			mTitleView.setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
			mTitleView.setText(title);//只设置颜色时不生效,加上设置title时才会生效,原因不明
		} catch (Exception ignore) {
		}
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) userName.getLayoutParams();
		int top = DisplayUtil.dp2px(context, 5);
		int left = DisplayUtil.dp2px(context, 20);
		params.setMargins(left, top, left, top);
		userName.setLayoutParams(params);
		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.gray_text));
		alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(context, R.color.md_dialog_text));
		alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			@Override
			public void onShow(DialogInterface dialog) {
//				InputMethodManager manager = (InputMethodManager) userName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//				manager.showSoftInput(control_input_pass, 0);
				//设置可获得焦点
				userName.setFocusable(true);
				userName.setFocusableInTouchMode(true);
				//请求获得焦点
				userName.requestFocus();
				UIUtil.showPan(userName);
			}
		});
		return alertDialog;
	}


	public interface OnBtnClickListener {
		void onClick(DialogInterface dialog, int which);
	}

	public interface OnMultiSelectListener {
		void onMultiSelect(DialogInterface dialog, int which, boolean isChecked);
	}

	public interface OnConfirmListener {
		void onConfirm(DialogInterface dialog, String content, EditText userName);
	}
}
